package xzit.gcxy.xxhzx.zhh.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import xzit.gcxy.xxhzx.zhh.client.Client;
import xzit.gcxy.xxhzx.zhh.common.AjaxJson;
import xzit.gcxy.xxhzx.zhh.common.Pagination;
import xzit.gcxy.xxhzx.zhh.common.ResourceUtil;
import xzit.gcxy.xxhzx.zhh.entity.ManagedAccountVO;
import xzit.gcxy.xxhzx.zhh.entity.ScienceWorkTypeVO;
import xzit.gcxy.xxhzx.zhh.service.ScienceWorkTypeService;

@Controller
@RequestMapping("/scienceWorkTypeController")
public class ScienceWorkTypeController {
	private static final Logger logger = Logger.getLogger(ScienceWorkTypeController.class);

	@Autowired
	private ScienceWorkTypeService scienceWorkTypeService;

	/**
	 * 科研工作量详情页
	 * 
	 * @param error
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "goSWType")
	public ModelAndView goSWDetail(HttpServletRequest request) {
		return new ModelAndView("app/swtype");
	}

	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(HttpServletRequest request, HttpServletResponse response, ScienceWorkTypeVO scienceWorkTypeVO)
			throws Exception {
		AjaxJson j = new AjaxJson();

		j.setMsg("保存成功！");
		j.setSuccess(true);
		Client client = ResourceUtil.getClient();
		ManagedAccountVO user = client.getUser();
		try {
			scienceWorkTypeVO.setInstituteid(user.getInstitute().getInstituteid());
			this.scienceWorkTypeService.save(scienceWorkTypeVO);
		} catch (Exception e) {
			j.setMsg("保存失败！");
			j.setSuccess(false);
		}

		return j;

	}

	@RequestMapping(params = "update")
	@ResponseBody
	public AjaxJson update(HttpServletRequest request, HttpServletResponse response, ScienceWorkTypeVO scienceWorkTypeVO)
			throws Exception {
		AjaxJson j = new AjaxJson();
		j.setMsg("保存成功！");
		j.setSuccess(true);
		try {
			this.scienceWorkTypeService.update(scienceWorkTypeVO);
		} catch (Exception e) {
			j.setMsg("保存失败！");
			j.setSuccess(false);
		}
		return j;

	}

	@RequestMapping(params = "datagrid")
	@ResponseBody
	public void datagrid(HttpServletRequest request, HttpServletResponse response, ScienceWorkTypeVO ve, String gonghao)
			throws Exception {
		String page = request.getParameter("page");// easyui datagrid 分页 页号
		String rows = request.getParameter("rows");// easyui datagrid 分页 页数
		if (page == null) {
			page = "0";
		}
		if (rows == null) {
			rows = "0";
		}

		DetachedCriteria condition = DetachedCriteria.forClass(ScienceWorkTypeVO.class);
		Pagination<?> pagination = scienceWorkTypeService.findPageData(condition, ve, Integer.parseInt(page),
				Integer.parseInt(rows));

		JSONObject jobj = new JSONObject();
		jobj.put("total", pagination.getTotalCount());
		jobj.put("rows", pagination.getDatas());

		response.setCharacterEncoding("utf-8");
		response.getWriter().write(jobj.toString());

	}
}
